package cn.edu.jmu.oelp.controller;

import cn.edu.jmu.oelp.pojo.Entity;
import cn.edu.jmu.oelp.pojo.ReqEntity;
import cn.edu.jmu.oelp.pojo.RespCode;
import cn.edu.jmu.oelp.pojo.RespEntity;
import cn.edu.jmu.oelp.service.ArticleService;
import cn.edu.jmu.oelp.service.EntityNeo4jService;
import cn.edu.jmu.oelp.service.EntityService;
import cn.edu.jmu.oelp.service.ParagraphsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author hhb damao
 * 对标注实体增删改查的控制器，如果在mysql上事务提交成功，则对neo4j进行操作
 * neo4j的service还未进行异常处理
 */
@RestController
@Transactional(rollbackFor=Exception.class)
public class EntityController {
    @Autowired
    private EntityService entityService;
    @Autowired
    private EntityNeo4jService entityNeo4jService;
    @Autowired
    private ParagraphsService paragraphsService;
    @Autowired
    private ArticleService articleService;
    
    @RequestMapping("/addEntity")
    public RespEntity addEntity(@RequestBody ReqEntity reqEntity){
        try{
            Entity entity;
            if (reqEntity!=null) {
                entity = new Entity(reqEntity.getParagraphsId(), reqEntity.getArticleId(), reqEntity.getContent(), reqEntity.getType(),reqEntity.getUserId());
                int entityId = entityService.addEntity(entity);
                if (!String.valueOf(entityId).equals("")) {
                    System.out.println(entity);
                    if(entityNeo4jService.addEntity(entity) != null)
                    {
                        System.out.println("add to neo4j");
                        return new RespEntity(RespCode.SUCCESS, entity);
                    }
                }else {
                    System.out.println("id null");
                }
                return new RespEntity(RespCode.ADDERROR);
            }
        }catch (Exception e){
            System.out.println(e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return new RespEntity(RespCode.ADDERROR);
    }

    @RequestMapping("/delEntity")
    public RespEntity delEntity(@RequestParam("entityId")int entityId){
        try {
            if (entityService.delEntity(entityId)){
                System.out.println("delete");
                entityNeo4jService.delEntity((long) entityId);
                return new RespEntity(RespCode.SUCCESS);
            }else {
                System.out.println("not delete");
                return new RespEntity(RespCode.DELERROR);
            }
        }catch (Exception e){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return new RespEntity(RespCode.DELERROR);
    }
    @RequestMapping("/updateEntity")
    public RespEntity updateEntity(@RequestBody Entity entity){
        try {
            if (entityService.updateEntity(entity)){
                System.out.println(entity);
                entityNeo4jService.updateEntity(entity);
                return new RespEntity(RespCode.SUCCESS);
            }else {
                return new RespEntity(RespCode.UPDATEERROR);
            }
        }catch (Exception e){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return new RespEntity(RespCode.UPDATEERROR);
    }
    @RequestMapping("/selEntityById")
    public RespEntity selEntityById(@RequestParam("entityId") int entityId){
        try {
            Entity entity = entityNeo4jService.findByEntityId(entityId);
            return new RespEntity(RespCode.SUCCESS,entity);
        }catch (Exception e){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return new RespEntity(RespCode.SELERROR);
    }

    @RequestMapping("/submitEntity")
    public RespEntity submitEntity(@RequestBody List<ReqEntity> reqEntityList){
        try{
            if (reqEntityList!=null) {
                List<Entity> entityList =
                        entityNeo4jService.findByArticleIdAndParagraphsIdAndUserId(reqEntityList.get(0).getArticleId(),
                                        reqEntityList.get(0).getParagraphsId(),reqEntityList.get(0).getUserId());
                List<Entity> newEntityList = new ArrayList<>();
                for(ReqEntity reqEntity: reqEntityList)
                {
                    Entity entity = new Entity(reqEntity.getParagraphsId(), reqEntity.getArticleId(),
                                                reqEntity.getContent(), reqEntity.getType(),reqEntity.getUserId());
                    entity.setEntityId(reqEntity.getEntityId());
                    if(!(entityList.contains(entity)))    /*库中不存在该标注，则添加*/
                    {
                        int entityId = entityService.addEntity(entity);
                        if (!String.valueOf(entityId).equals("")) {
                            System.out.println(entity);
                            if(entityNeo4jService.addEntity(entity) != null)
                            {
                                System.out.println("add to neo4j");
                                newEntityList.add(entity);
                            }
                        }else {
                            System.out.println("id null, add error");
                            return new RespEntity(RespCode.ADDERROR);
                        }
                    }
                    else    /*库中存在该实体，则更新为新传入的标注*/
                    {
                        if(entityService.updateEntity(entity))
                        {
                            entityNeo4jService.updateEntity(entity);
                            entityList.remove(entity);
                            newEntityList.add(entity);
                            System.out.println("update");
                        }
                        else{
                            System.out.println(entity);
                            return new RespEntity(RespCode.UPDATEERROR);
                        }
                    }
                }
                for(Entity delEntity: entityList)   /*把库里原本有的，没有提交的标注删除*/
                {
                    entityService.delEntity(delEntity.getEntityId());
                    entityNeo4jService.delEntity((long) delEntity.getEntityId());
                }
                return new RespEntity(RespCode.SUCCESS, newEntityList);
            }
        }catch (Exception e){
            System.out.println(e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return new RespEntity(RespCode.ADDERROR);
    }
}
